from business.register.register import h5_register
from base.tools import wrapper
from business.h5buyproduct.buy import BuyProduct
from base.request_new import RequestNew
from business.ssologin.login import user_login

mozi = RequestNew(cookie=user_login())


@wrapper
def new_users(product_list, base_mobile, count=1, only_register=False, rename=True):
    res = list()
    for i in range(count):
        mobile = base_mobile + i
        uid, cookies = h5_register(mobile)
        user_rename(mobile, uid) if rename else None
        if only_register:
            return {"userId": uid}
        bp = BuyProduct(cookies=cookies)
        for product in product_list:
            if isinstance(product, (tuple, list)):
                r, v = bp.buy_product_pack_by_h5(*product)
            elif isinstance(product, str):
                r, v = bp.buy_product_pack_by_h5(product_pack_id=product)
            elif isinstance(product, dict):
                r, v = bp.buy_product_pack_by_h5(**product)
            else:
                res.append({mobile: {"product_id": product, "ERROR": "类型错误，未购买"}})
                continue
            res.append({mobile: v})
    return res


@wrapper
def user_rename(mobile, user_id):
    req = {
        "url": "",
        "data": {
            "userId": user_id,
            "name": f"{str(mobile)[3:5]}..{str(mobile)[-2:]}"
        },
        "method": "POST",
        "content_type": "FORM"
    }
    res, v = mozi.request_auto(**req)
    return res


product_list= [
    # 商品id，版本
    ("", ""),
    # 
    ("", ""),
    # 
    ("", ""),
    # 
    ("", "")
]


if __name__ == '__main__':
    new_users(product_list, base_mobile=19950000062, count=1)
